import { App, epclass, navigate } from '../../library/api/index'
var check = 'https://r.epclass.meadjohnson.joypaw.com/p/v2/check.png'
var uncheck = 'https://r.epclass.meadjohnson.joypaw.com/p/v2/uncheck.png'
var agree = 'https://r.epclass.meadjohnson.joypaw.com/p/v2/agree.png'
Page({
  data: {
    choose_brand: '',
    choose_care: '',
    has_second_baby: true,
    mobile: '',
    verify_code: '',
    baby_birthday: '',
    second_baby_birthday: '',
    brand: '正在给宝宝使用的奶粉品牌',
    qt: '选择宝宝配方奶粉关注什么问题',
    brand2: '您正在给二胎宝宝使用的奶粉品牌',
    qt2: '选择二胎宝宝配方奶粉关注什么问题',
    second_baby: '+ 我有二胎宝宝',
    intentions: '',
    intentions2: '',
    attentions: '',
    attentions2: '',
    items: [],
    item_qus: [],
    isExistSecondBaby: 0,
    agree_i: agree,
    cid: '',
    tip: 'blank',
    tipData: {},
    end_date: '',
    url: '',
    isEpclass: true
  },
  onLoad: function(e) {
    if(e.form && e.from.indexOf('mall') > -1) {
      this.data.isEpclass = false
    }
    this.setData({
      cid: e.cid,
      isEpclass: this.data.isEpclass,
      url: decodeURIComponent(e.from)
    })
    App.jice.onLoad(e)
  },
  onReady: function() {
    var now = new Date()
    var nowTime = now.getTime()
    var maxTime = nowTime + 549 * 24 * 3600 * 1000
    var max = new Date(maxTime)
    this.data.end_date = max.getFullYear() + '-' + (max.getMonth() + 1) + '-' + max.getDate()
    this.setData({
      end_date: this.data.end_date
    })
    this.get()
    App.jice.track('page', {
      pagepath: '/pages/register/index',
      pageTitle: '注册',
      pageType: 'view'
    })
    App.logger({
      type: App.G.view,
      channel: App.G.channel,
      target: '注册'
    })
  },
  onShow: function() {
    App.jice.track('appshow')
  },
  get: function() {
    epclass.getAttentions({}, r => {
      if(r.code === 200) {
        for(var i = 0; i < r.data.brand.length; i++) {
          this.data.items.push({
            value: r.data.brand[i].k,
            text: r.data.brand[i].v,
            src: uncheck
          })
        }
        for(var i = 0; i < r.data.question.length; i++) {
          this.data.item_qus.push({
            value: r.data.question[i].k,
            text: r.data.question[i].v,
            src: uncheck
          })
        }
      }
    })
  },
  verifyCode: function() {
    if(this.data.mobile.length == 0) {
      this.showErrorTip('你有消息未填写')
      return
    }
    epclass.getVerifyCode({mobile: this.data.mobile}, r => {
      this.showErrorTip(r.msg)
    })
  },
  listenerCode: function(e) {
    this.data.verify_code = e.detail.value
    if(this.data.verify_code.length == 0) {
      return
    }
    epclass.checkVerifyCode({
      verify_code: this.data.verify_code,
      mobile: this.data.mobile
    }, r => {
      if(r.code != 200) {
        if(r.code == 501) {
          this.showBackTip(r.msg)
        } else {
          this.showErrorTip(r.msg)
        }
      }
    })
  },
  submit: function(e) {
    var value = e.detail.value
    var post = {
      form_id: e.detail.formId,
      class_id: this.data.cid,
      mobile: this.data.mobile,
      verify_code: this.data.verify_code,
      name: value.name,
      code: value.code,
      baby_birthday: this.data.baby_birthday,
      intentions: this.data.intentions,
      attentions: this.data.attentions,
      second_baby_birthday: this.data.second_baby_birthday,
      second_baby_intention: this.data.intentions2,
      second_baby_attention: this.data.attentions2,
      isExistSecondBaby: this.data.isExistSecondBaby
    }
    epclass.register(post, r => {
      if(r.code == 201) {
        this.showSuccessRegister()
      } else {
        this.showErrorTip(r.msg)
      }
    })
  },
  showSuccessRegister: function() {
    this.setData({
      tip: 'success_register',
      tipData: {}
    })
  },
  back: function() {
    navigate.to(this.data.url)
  },
  listenerMobile: function(e) {
    this.data.mobile = e.detail.value
  },
  showErrorTip: function(msg) {
    if(this.data.isEpclass) {
      this.setData({
        tip: 'error_tip',
        tipData: msg
      })
    } else {
      this.setData({
        tip: 'no_fill',
        tipData: msg
      })
    }
  },
  showBackTip: function(msg) {
    this.setData({
      tip: 'back_tip',
      tipData: msg
    })
  },
  closeTip: function() {
    this.setData({
      tip: 'blank',
      tipData: {}
    })
  },
  clause: function() {
    this.setData({
      tip: 'clause',
      tipData: {}
    })
  },
  chooseCare: function(e) {
    if(this.data.choose_care != e.target.id) {
      for(var i = 0; i < this.data.item_qus.length; i++) {
        this.data.item_qus[i].src = uncheck
      }
      if(e.target.id == 'care') {
        if(this.data.attentions.length > 0) {
          for(var i = 0; i < this.data.attentions.length; i++) {
            for(var j = 0; j < this.data.item_qus.length; j++) {
              if(this.data.attentions[i] == this.data.item_qus[j].value) {
                this.data.item_qus[j].src = check
              }
            }
          }
        }
      } else {
        if(this.data.attentions2.length > 0) {
          for(var i = 0; i < this.data.attentions2.length; i++) {
            for(var j = 0; j < this.data.item_qus.length; j++) {
              if(this.data.attentions2[i] == this.data.item_qus[j].value) {
                this.data.item_qus[j].src = check
              }
            }
          }
        }
      }
    }
    this.data.choose_care = e.target.id
    if(this.data.choose_care == 'care') {
      if(this.data.qt == '选择宝宝配方奶粉关注什么问题') {
        this.data.qt = ''
      }
    } else {
      if(this.data.qt2 == '选择二胎宝宝配方奶粉关注什么问题') {
        this.data.qt2 = ''
      }
    }
    this.setData({
      tip: 'attention',
      tipData: {
        item_qus: this.data.item_qus
      }
    })
  },
  chooseBrand: function(e) {
    if(this.data.choose_brand != e.target.id) {
      for(var i = 0; i < this.data.items.length; i++) {
        this.data.items[i].src = uncheck
      }
      if(e.target.id == 'brand') {
        if(this.data.intentions.length > 0) {
          for(var i = 0; i < this.data.items.length; i++) {
            if(this.data.intentions[0] == this.data.items[i].value) {
              this.data.items[i].src = check
            }
          }
        }
      } else {
        if(this.data.intentions2.length > 0) {
          for(var i = 0; i < this.data.items.length; i++) {
            if(this.data.intentions2[0] == this.data.items[i].value) {
              this.data.items[i].src = check
            }
          }
        }
      }
    }
    this.data.choose_brand = e.target.id
    this.setData({
      tip: 'intention',
      tipData: {
        items: this.data.items
      }
    })
  },
  has: function() {
    if(this.data.has_second_baby) {
      this.setData({
        isExistSecondBaby: 1,
        has_second_baby: false,
        second_baby: '- 取消二胎宝宝'
      })
    } else {
      this.setData({
        isExistSecondBaby: 0,
        has_second_baby: true,
        second_baby: '+ 我有二胎宝宝'
      })
    }
  },
  bindCheckbox1: function(e) {
    var index = parseInt(e.currentTarget.dataset.index)
    var type = this.data.items[index].src
    var items = this.data.items
    if(this.data.choose_brand == 'brand') {
      if(type == uncheck) {
        if(this.data.intentions.length > 0) {
          this.showErrorTip('只能选择一个奶粉品牌呦~')
          return
        }
        items[index].src = check
      } else {
        items[index].src = uncheck
      }
      this.setData({
        tipData: {
          items: items
        }
      })
      var checkedValues = []
      for(var i = 0; i < items.length; i++) {
        if(items[i].src == check) {
          checkedValues.push(items[i].value)
        }
      }
      this.setData({
        intentions: checkedValues,
        brand: items[index].text
      })
    } else {
      if(type == uncheck) {
        if(this.data.intentions2.length > 0) {
          this.showErrorTip('只能选择一个奶粉品牌呦~')
          return
        }
        items[index].src = check
      } else {
        items[index].src = uncheck
      }
      this.setData({
        tipData: {
          items: items
        }
      })
      var checkedValues = []
      for(var i = 0; i < items.length; i++) {
        if(items[i].src == check) {
          checkedValues.push(items[i].value)
        }
      }
      this.setData({
        intentions2: checkedValues,
        brand2: items[index].text
      })
    }
  },
  bindCheckbox2: function(e) {
    var index = parseInt(e.currentTarget.dataset.index)
    var type = this.data.item_qus[index].src
    var item_qus = this.data.item_qus
    if(this.data.choose_care == 'care') {
      if(type == uncheck) {
        if(this.data.qt.length >= 3) {
          this.showErrorTip('最多选择三个关注问题呦~')
          return
        }
        item_qus[index].src = check
      } else {
        item_qus[index].src = uncheck
      }
      this.setData({
        tipData: {
          item_qus: item_qus
        }
      })
      var checkedValues2 = []
      for(var i = 0; i < item_qus.length; i++) {
        if(item_qus[i].src == check) {
          checkedValues2.push(item_qus[i].text)
        }
      }
      this.setData({
        qt: checkedValues2,
        attentions: item_qus[index].value
      })
    } else {
      if(type == uncheck) {
        if(this.data.qt2.length >= 3) {
          this.showErrorTip('最多选择三个关注问题呦~')
          return
        }
        item_qus[index].src = check
      } else {
        item_qus[index].src = uncheck
      }
      this.setData({
        tipData: {
          item_qus: item_qus
        }
      })
      var checkedValues2 = []
      for(var i = 0; i < item_qus.length; i++) {
        if(item_qus[i].src == check) {
          checkedValues2.push(item_qus[i].text)
        }
      }
      this.setData({
        qt2: checkedValues2,
        attentions2: item_qus[index].value
      })
    }
  },
  bindDateChange: function(e) {
    if(e.target.id == 'baby_birthday') {
      this.setData({
        baby_birthday: e.detail.value
      })
    } else {
      this.setData({
        second_baby_birthday: e.detail.value
      })
    }
  },
  toBrand: function(e) {
    if(this.data.choose_brand == 'brand') {
      if(this.data.intentions.length == 0) {
        this.showErrorTip('奶粉品牌不能为空呦~')
        return
      }
    } else {
      if(this.data.intentions2.length == 0) {
        this.showErrorTip('二胎宝宝奶粉品牌不能为空呦~')
        return
      }
    }
    this.setData({
      tip: 'blank',
      tipData: {}
    })
  },
  toQuestion: function(e) {
    if(this.data.choose_care == 'care') {
      if(this.data.attentions.length == 0) {
        this.showErrorTip('关注问题不能为空呦~')
        return
      }
    } else {
      if(this.data.attentions2.length == 0) {
        this.showErrorTip('二胎宝宝关注问题不能为空呦~')
        return
      }
    }
    this.setData({
      tip: 'blank',
      tipData: {}
    })
  },
  onShareAppMessage: function() {
    App.jice.track('onshare', {
      shareto: 'friend',
      title: '【专家学堂-美赞臣中国】与专家对话，做A+妈妈',
      path: '/pages/register/index'
    })
    return {
      title: '【专家学堂-美赞臣中国】与专家对话，做A+妈妈',
      desc: '听了这些资深专家的话，你也能成为A+妈妈哟~还不快来报名？',
      path: '/pages/register/index',
      success: function() {
        epclass.shareLogger({
          class_id: 0,
          target: '用户注册',
          action: 'appMessage'
        }, r => {
        })
      }
    }
  }
})
